package com.schevio.chinesesoc;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

public class ImageRotaionActivity extends Activity implements  android.view.View.OnTouchListener{  
	 private ImageView imageview;  
	 private ViewGroup mContainer;  
	 private boolean isCardFront=true;
	 private boolean rotateLeft=false;
	 private int screenWidth=0;
	 private boolean isActivated=true;// It is activated by default.
	 private Bitmap bitmap2=null;
	 
 private int rotationDegree=0;
	 private int xx, yy, zz;
		SensorManager sensorMgr;
	 private static final int IMAGE[] = new int[]{
		 R.drawable.membership_card_page_1,
		 R.drawable.membership_card_page_2
	 };
	 private BitmapDrawable bmp=null;
	 
	    /** Called when the activity is first created. */  
	    @Override  
	    public void onCreate(Bundle savedInstanceState) {  
	        super.onCreate(savedInstanceState);  
	        setContentView(R.layout.main); 
	       
    
			imageview = (ImageView) findViewById(R.id.image1);  
	        
	        mContainer = (ViewGroup) findViewById(R.id.container);
	        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.membership_card_page_1);
	      
	        imageview.setImageBitmap(this.writeTextOnCard(bitmap));
		         //将图片设置到中间
		         //imageview.setScaleType(ScaleType.CENTER);
		         //将图片填充之整个视图
		        
		       //  lly.addView(imageview, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		         //添加ImageView到布局模板中
	          
			   imageview.setClickable(true);  
		        imageview.setFocusable(true);  
		        imageview.setOnTouchListener(this); 
	        
	        // 得到设备的分辨率        
	        DisplayMetrics dm = new DisplayMetrics();        
	        getWindowManager().getDefaultDisplay().getMetrics(dm);        
	        int widthPixels = dm.widthPixels;        
	        int heightPixels = dm.heightPixels;        
	        float density = dm.density;        
	        // 计算屏幕宽度和高度        
	         screenWidth = (int) (widthPixels * density);        
	         int screenHeight = (int) (heightPixels * density);       
	             
	       
	         
	         
	        
	    }  
	    private void applyRotation(int position, float start, float end) {  
	        // Find the center of the container  
	        final float centerX = mContainer.getWidth() / 2.0f;  
	        final float centerY = mContainer.getHeight() / 2.0f; 
//	        Rotate3dAnimation leftAnimation = new Rotate3dAnimation(-0,-90,centerX,centerY,310.0f,true);
//			 Rotate3dAnimation rightAnimation=new Rotate3dAnimation(-0+90,-90+90,centerX,centerY,310.0f,true);
//			 leftAnimation.setFillAfter(true);
//			 leftAnimation.setDuration(1000);
//			 rightAnimation.setFillAfter(true);
//			 rightAnimation.setDuration(1000);
//			 imageview.startAnimation(leftAnimation);
//			 imageview2.startAnimation(rightAnimation);
	        if(isCardFront==true){
	        final Rotate3dAnimation rotation =  
	                new Rotate3dAnimation(start, end, centerX, centerY, 0, false);  
	        rotation.setDuration(300);  
	        rotation.setFillAfter(true);  
	        rotation.setInterpolator(new AccelerateInterpolator());  
	        rotation.setAnimationListener(new DisplayNextView(position));  
	        mContainer.startAnimation(rotation);  }
	        else{
	        	final Rotate3dAnimation rotation =  
	   	                new Rotate3dAnimation(start, end, centerX, centerY, 0, false);  
	   	        rotation.setDuration(300);  
	   	        rotation.setFillAfter(true);  
	   	        rotation.setInterpolator(new AccelerateInterpolator());  
	   	        rotation.setAnimationListener(new DisplayNextView(position));  
	   	        mContainer.startAnimation(rotation); 
	        }
	    }      
	    
		public boolean onTouch(View arg0, MotionEvent e) {
			// TODO Auto-generated method stub
	    	if(e.getX() > (mContainer.getWidth() / 2.0f)){ //this.screenWidth/2?
	    		applyRotation(0,0,-90);
	    		rotateLeft=true;
	    	}else{
	    		applyRotation(0,0,90);
	    		rotateLeft=false;
	    	}
			return true;
		}
 //Different People has differnt info. It does the job when the card is activated.
	    private Bitmap writeTextOnCard(Bitmap card){
	    	
	    	if(this.isActivated){
	    	   bitmap2=drawTextAtBitmap(card,getString(R.string.username),53,493);
		         bitmap2=drawTextAtBitmap(bitmap2,getString(R.string.userid),660,493);
		         
		         return bitmap2;
	    	}
	    	return card;
	    }
	    /**
		 * 图片上画字
		 * */
		private Bitmap drawTextAtBitmap(Bitmap bitmap,String text,int xpos,int ypos){
			
			int x = bitmap.getWidth();
			
			int y = bitmap.getHeight();
			
			// 创建一个和原图同样大小的位图
			Bitmap newbit = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
			
			Canvas canvas = new Canvas(newbit);
			
			Paint paint = new Paint();
			Typeface mFace;
			// 在原始位置0，0插入原图
			canvas.drawBitmap(bitmap, 0, 0, paint);
			
			paint.setColor(Color.parseColor("#000000"));
			//mFace = Typeface.createFromAsset(getAssets(),"penrunninghand.TTF");
			//paint.setTypeface(mFace);
			paint.setTextSize(45);
			
			// 在原图指定位置写上字
			canvas.drawText(text, xpos , ypos, paint);
			
			canvas.save(Canvas.ALL_SAVE_FLAG);
			
			// 存储
			canvas.restore();
			
			return newbit;
		}
		
	 private final class DisplayNextView implements Animation.AnimationListener {  
	        private final int mPosition;  
	        private DisplayNextView(int position) {  
	            mPosition = position;  
	        }  
	        public void onAnimationStart(Animation animation) {  
	        	 
	        }  
	        public void onAnimationEnd(Animation animation) {  
	        	 if(isCardFront==true){
			            imageview.setImageResource(IMAGE[1]);
			            isCardFront=false;
			            }else{
			            	if(bitmap2==null){ 
			            	Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.membership_card_page_1);
			            	imageview.setImageBitmap(bitmap);
			            	}else{imageview.setImageBitmap(bitmap2);}
			            	isCardFront=true;
			            }
	          mContainer.post(new SwapViews(mPosition));
	        }  
	        public void onAnimationRepeat(Animation animation) {  
	        }  
	    }  
	    /**  
	     * This class is responsible for swapping the views and start the second  
	     * half of the animation.  
	     */  
	    private final class SwapViews implements Runnable {  
	        private final int mPosition;  
	        public SwapViews(int position) {  
	            mPosition = position;  
	        }  
	        public void run() {  
	            final float centerX = mContainer.getWidth() / 2.0f;  
	            final float centerY = mContainer.getHeight() / 2.0f;  
	            Rotate3dAnimation rotation;  
	            imageview.setVisibility(View.VISIBLE);  
	            imageview.requestFocus();  
	            if(rotateLeft==true){
	            	rotation = new Rotate3dAnimation(-90, -180, centerX, centerY, 0, false); 
	            }
	            else{
	            	rotation = new Rotate3dAnimation(90, 180, centerX, centerY, 0, false);
	            rotateLeft=true;
	            } 
	            rotation.setDuration(300);  
	            rotation.setFillAfter(true);  
	            rotation.setInterpolator(new DecelerateInterpolator());  
	            mContainer.startAnimation(rotation);  
	          
	        }  
	    }
		
 
	} 